package com.sky.directdeal.activity;

import java.util.HashMap;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.sky.directdeal.R;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.crouton.Style;
import com.sky.directdeal.listener.ServiceResponseListener;
import com.sky.directdeal.manager.ServiceManager;
import com.sky.directdeal.utils.Utils;
import com.sky.local_ads.create_tell_us.CreateTellUsResponse;

public class TelUsActivity extends BaseActivity {
	
	private boolean isService=false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tel_us);
		setTitle(getString(R.string.tell_us_title));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tel_us, menu);
		return true;
	}
	
	public void saveQuery(View view){
		EditText nameEditText=(EditText)findViewById(R.id.editTextName);
		String name=nameEditText.getText().toString();
		
		EditText phoneEditeText=(EditText)findViewById(R.id.editTextPhNo);
		String phone=phoneEditeText.getText().toString();
		
		EditText emailEditText=(EditText)findViewById(R.id.editTextEmail);
		String email=emailEditText.getText().toString();
		
		EditText messageEditText=(EditText)findViewById(R.id.editTextMessage);
		String message=messageEditText.getText().toString();
		
		if(TextUtils.isEmpty(name) && name!=null){
			Utils.displayToast(TelUsActivity.this, getResources().getString(R.string.enter_name), Style.INFO);
			
		}else if(TextUtils.isEmpty(phone) && phone!=null){
			Utils.displayToast(TelUsActivity.this, getResources().getString(R.string.enter_contact_no), Style.INFO);	
		}else if(TextUtils.isEmpty(message) && message!=null){
			Utils.displayToast(TelUsActivity.this, getResources().getString(R.string.enter_message), Style.INFO);
		}else{
			if(!isService){
				isService=true;
				mainCategoryHandler.sendEmptyMessage(Constants.SHOW_PROGRESS_DIALOG);
				HashMap<String, String> params=new HashMap<String, String>();
				params.put("name", name);
				params.put("phone", phone);
				params.put("email", email);
				params.put("message", message);
				ServiceManager.putTellUsQuery(getApplicationContext(), params, createQueryListener);
			}
			
		}
		
	}
	
	private ServiceResponseListener<CreateTellUsResponse> createQueryListener=new ServiceResponseListener<CreateTellUsResponse>() {
		
		@Override
		public void onSuccess(CreateTellUsResponse response) {
			isService=false;;
			mainCategoryHandler.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
			showAlert(TelUsActivity.this, getString(R.string.alert), getString(R.string.saved_successfully), getString(R.string.ok_button), positiveListener, null, null);
			
		}
		
		@Override
		public void onFailure(Throwable throwable, String errorResponse) {
			isService=false;;
			mainCategoryHandler.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
			showAlert(TelUsActivity.this, getString(R.string.alert), getString(R.string.saving_failed), getString(R.string.ok_button), positiveListener, null, null);
			
		}
	};
	
	 public DialogInterface.OnClickListener positiveListener=new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		};

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
